package ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.impl;

import java.util.Date;
import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.DeviceDAO;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.AddressType;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.AddressValue;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.Alarm;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.CompoundCommand;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.Device;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.DeviceType;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.Field;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.SimpleCommand;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.SoundRequest;

public class DeviceDAOImpl extends HibernateDaoSupport implements DeviceDAO{

	@Override
	public Device findDevice(Integer id) {
		return (Device)getHibernateTemplate().get(Device.class, id);
	}

	@Override
	public String getDeviceTypeName(Device dev) {
		DeviceType dt = dev.getType();
		return ((DeviceType)getHibernateTemplate().get(DeviceType.class,dt.getID())).getName();		
	}

	@Override
	public Date getLastActivity(Device dev) {
		return ((Device) getHibernateTemplate().get(Device.class, dev.getID())).getLastActivity();
	}

	@Override
	public List<Field> getFields(Device dev) {
		DeviceType dt = dev.getType();
		
		return (List<Field>)
				((DeviceType)
						(getHibernateTemplate().get(DeviceType.class, dt.getID())))
							.getFields();
	}

	@Override
	public List<Alarm> getAlarms(Device dev) {
		DeviceType dt = dev.getType();
		
		return (List<Alarm>)
				((DeviceType)
						(getHibernateTemplate().get(DeviceType.class, dt.getID())))
							.getAlarms();
	}

	@Override
	public List<CompoundCommand> getCompoundCommands(Device dev) {
		DeviceType dt = dev.getType();
		
		return (List<CompoundCommand>)
				((DeviceType)
						(getHibernateTemplate().get(DeviceType.class, dt.getID())))
							.getCompoundCommands();
	}

	@Override
	public List<SimpleCommand> getSimpleCommands(Device dev) {
		DeviceType dt = dev.getType();
		
		return (List<SimpleCommand>)
				((DeviceType)
						(getHibernateTemplate().get(DeviceType.class, dt.getID())))
							.getSimpleCommands();
	}

	@Override
	public List<SoundRequest> getSoundRequests(Device dev) {
		DeviceType dt = dev.getType();
		
		return (List<SoundRequest>)
				((DeviceType)
						(getHibernateTemplate().get(DeviceType.class, dt.getID())))
							.getSoundRequests();
	}

	@Override
	public Date lastDateActivity(Device device) {		
		return ((Device)getHibernateTemplate().get(Device.class,device.getID())).getLastActivity();
	}

	@SuppressWarnings("unchecked")
	@Override
	public AddressValue getLastAddressValue(Device device, Integer address,
			AddressType addressType) {
		String query = "select AV.* from Device D, AddressValue AV "
				+"where D.id = ? ORDER BY AV.date DESC";
		List<AddressValue> list = (List<AddressValue>)getHibernateTemplate().find(query, device.getID());
		
		return list.get(list.size()-1);
	}


	@Override
	public void saveValue(Device device, Integer address, Integer value,
			AddressType addressType) {
		
		AddressValue av = new AddressValue();
		av.setAddress(address);
		av.setAddressType(addressType);
		av.setValue(value);
		av.setDevice(device);
		av.setDate(device.getLastActivity());
		
		getHibernateTemplate().save(av);		
		
	}

	@Override
	public void setLastActivity(Device device, Date date) {
		device.setLastActivity(date);
		getHibernateTemplate().saveOrUpdate(device);
	}

	@Override
	public void saveValues(Device device, List<AddressValue> addValues) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<DeviceType> listDeviceTypes() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void saveOrUpdateType(DeviceType type) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void deleteType(DeviceType type) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<Device> listDevices() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void saveOrUpdateDevice(Device type) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void deleteDevice(Device type) {
		// TODO Auto-generated method stub
		
	}

	}
